package cn.tedu.v1.controller;


import cn.tedu.v1.exception.ServiceException;
import cn.tedu.v1.model.DepartmentOffice;
import cn.tedu.v1.model.Doctors;
import cn.tedu.v1.service.impl.DoctorsServiceImpl;
import cn.tedu.v1.vo.DoctorVo;
import cn.tedu.v1.vo.UpdateDocVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 医生列表 前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/portal/doctors")
@Slf4j
public class DoctorsController {

    @Resource
    private DoctorsServiceImpl doctorsService;
    @PostMapping("/add_doctor")
    public String addDoctor(@Validated DoctorVo doctorVo, BindingResult result) {
        if (result.hasErrors()) {
            String msg = result.getFieldError().getDefaultMessage();
            return msg;
        } else {
            log.debug("上传的医生信息:{}", doctorVo);
            String msg = doctorsService.addDoctor(doctorVo);
            if (msg.equals("1")) {
                return "添加成功";
            }else{
                return msg;
            }
        }

    }
    @GetMapping("/getalldoctors")
    public List<Doctors> getAllDoctors(){
        List doctorslist=doctorsService.getAllDoctors();
        log.debug("所有的医生信息:{}",doctorslist);
        return doctorslist;
    }

    //更新医生信息
    @PostMapping("/updatedoctors")
    public String UpdateDoctors(@Validated UpdateDocVo updateDocVo,BindingResult result){
        log.debug("修改的医生医生信息:{}",updateDocVo);
        String msg=doctorsService.updateDoctor(updateDocVo);
        return msg;
    }

    //删除某个医生
    @GetMapping("/deleteDoc")
    public String deleteDoc(Integer id){
        String msg=doctorsService.deleteDoctor(id);
        return msg;
    }

    @GetMapping("/getdoctorsbyofficeid")
    public List<Doctors> getDoctorsByOfficeId(Integer id){
        List<Doctors> doctors=doctorsService.getDoctorsByOfficeId(id);
        return doctors;
    }
}
